﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using WinnerCMS.BLL.PageFactory.ManageHtml;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    public class PhotoManage
    {
        private static readonly PhotoDal Dal = new PhotoDal();

        #region 添加图片

        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="model"></param>
        /// <param name="tempFlag"></param>
        /// <param name="fields"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        [Logging, ExceptionCall]
        public Result Add(Photo model, string tempFlag, IList<string> fields, IList<object> values)
        {
            Result resu = new Result();
            if (model.ContentTitle.Length == 0)
            {
                resu.Errors = "请添加了图片标题以后再添加！";
                return resu;
            }

            if (model.Content.Trim().Length == 0)
            {
                resu.Errors = "请填写了图片内容以后再添加！";
                return resu;
            }
            if (CategoryManage.CheckAllowCategory(model.CategoryId) == false)
            {
                resu.Errors = "您没有该栏目的录入权限！";
                return resu;
            }

            if (string.IsNullOrEmpty(model.Author))
            {
                model.Author = "不详";
            }

            if (string.IsNullOrEmpty(model.ComeFrom))
            {
                model.ComeFrom = "不详";
            }
            else
            {
                model.ComeFrom = HTML.NoHtml(model.ComeFrom);
            }
            if (string.IsNullOrEmpty(model.Brief))
            {
                model.Brief = MyString.Left(HTML.NoHtml(model.Content), 255);
            }
            if (model.Tags.Contains("|") == false)
            {
                model.Tags = model.Tags + "|";
            }
            if (string.IsNullOrWhiteSpace(model.TemplateFileName))
            {
                model.TemplateFileName = CategoryManage.GetInfo(model.CategoryId).ShowPageTemplateFileName;
            }
            if (string.IsNullOrEmpty(model.CategoryIds)) //如果CategoryIds没有赋值就用CategoryId给它唯一的一个栏目
            {
                model.CategoryIds = model.CategoryId.ToString();
            }

            if (model.Id == 0)
            {
                #region

                resu.Id = Dal.Add(model);


                HttpContext.Current.Session["Key"] = model.Tags;

                resu.LogMessage = "添加图集：" + model.ContentTitle;

                //保存Tags
                new TagsManage().Add(model, UserManage.Current.UserID);

                if (tempFlag != null)
                    new PhotoListManage().FinishAction(model.Id, tempFlag);

                MakeSinglePage.ContentPageHTML(resu.Id);

                return resu;

                #endregion
            }

            #region

            Dal.Edit(model);

            DeleteHTML.ContentPage(model.Id);

            resu.Id = model.Id;

            HttpContext.Current.Session["Key"] = model.Tags;

            resu.LogMessage = "修改图集：" + model.ContentTitle;


            //保存Tags
            new TagsManage().Add(model, UserManage.Current.UserID);

            MakeSinglePage.ContentPageHTML(model.Id);

            return resu;

            #endregion
        }

        #endregion

        #region 加载图片

        /// <summary>
        /// 加载图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Photo GetInfo(int id)
        {
            return Dal.GetInfo(id);
        }

        #endregion
    }
}